package com.system.controller;

import com.system.model.SysRole;
import com.system.model.SysUser;
import com.system.model.SysUserRole;
import com.system.service.SysRoleService;
import com.system.service.SysUserRoleService;
import com.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sysRoleController")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;


    /**
     * 查询角色列表
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysRole
     * @return
     */
    @RequestMapping("/sysRoleList")
    public String sysRoleList(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole){
        //接收数据
        //执行业务
        //跳转页面
        List<SysRole> sysRoleList = sysRoleService.queryAllSysRole();
        session.setAttribute("sysRoleList",sysRoleList);
        return "view/system/sysRole/sysRoleList";
    }

    /**
     * 添加角色信息
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysRole
     * @return
     */
    @RequestMapping("/sysRoleAdd")
    public String sysRoleAdd(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole){
        //接收数据
        //执行业务
        //跳转页面
        int i = sysRoleService.sysRoleAdd(sysRole);
        if(i!=0){
            List<SysRole> sysRoleList = sysRoleService.queryAllSysRole();
            session.setAttribute("sysRoleList",sysRoleList);
            return "view/system/sysRole/sysRoleList";
        }
        model.addAttribute("message","添加角色失败！");
        return "view/system/sysRole/sysRoleAdd";
    }

    /**
     * 角色信息修改UI（传递信息）
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysRole
     * @param sysRoleIdStr
     * @return
     */
    @RequestMapping("/sysRoleUpdateUI")
    public String sysRoleUpdateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String sysRoleIdStr){
        //接收数据
        //执行业务
        //跳转页面
        SysRole sysRole_db= sysRoleService.sysRoleUpdateUI(sysRoleIdStr);
        if(sysRole_db!=null){
            model.addAttribute("sysRole_db",sysRole_db);
            return "view/system/sysRole/sysRoleUpdate";
        }
        return "view/system/sysRole/sysRoleList";
    }

    /**
     * 修改角色信息
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysRole
     * @return
     */
    @RequestMapping("/sysRoleUpdate")
    public String sysRoleUpdate(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole){
        //接收数据
        //执行业务
        //跳转页面
        int i= sysRoleService.sysRoleUpdate(sysRole);
        if(i!=0){
            List<SysRole> sysRoleList = sysRoleService.queryAllSysRole();
            session.setAttribute("sysRoleList",sysRoleList);
            return "view/system/sysRole/sysRoleList";
        }
        model.addAttribute("message","修改角色信息失败！");
        return "view/system/sysRole/sysRoleUpdate";
    }

    /**
     * 删除角色信息
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysRole
     * @param sysRoleIdStr
     * @return
     */
    @RequestMapping("/sysRoleDelete")
    public String sysRoleDelete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String sysRoleIdStr){
        //接收数据
        //执行业务
        //跳转页面
        int i= sysRoleService.sysRoleDelete(sysRoleIdStr);
        if(i!=0){
            List<SysRole> sysRoleList = sysRoleService.queryAllSysRole();
            session.setAttribute("sysRoleList",sysRoleList);
            return "view/system/sysRole/sysRoleList";
        }
        model.addAttribute("message","删除角色信息失败！");
        return "view/system/sysRole/sysRoleList";
    }

    @RequestMapping("/FPRoleUI")
    public String FPRoleUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String sysUserIdStr) {
        //1.接收数据
        int sysUserId = Integer.parseInt(sysUserIdStr);
        //3.调用业务
        //a.用户数据
        SysUser sysUser_db = sysUserService.querySysUserByUserId(sysUserId);
        model.addAttribute("sysUser_db", sysUser_db);

        //b.分配角色数据
        List<SysRole> sysRoleList_db = sysRoleService.queryAllSysRole();
        model.addAttribute("sysRoleList_db", sysRoleList_db);

        SysUserRole sysUserRole_db=sysUserRoleService.querySysUserRoleByUserId(sysUserId);
        model.addAttribute("sysUserRole_db", sysUserRole_db);
        //2.跳转页面
        return "view/system/sysRole/FPRoleUI";
    }

    @RequestMapping("/FPRole")
    public String FPRole(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String sysRoleIdStr, String sysUserIdStr, SysUserRole sysUserRole){
        //1.接收数据
        int sysUserId = Integer.parseInt(sysUserIdStr);
        int sysRoleId = Integer.parseInt(sysRoleIdStr);
        //3.调用业务
        //-->中间表增加数据，先判断数据库有没有
        int m = sysUserRoleService.deleteSysUserRoleByUserId(sysUserId);

        //增加
        sysUserRole.setUserId(sysUserId);
        sysUserRole.setRoleId(sysRoleId);
        int n = sysUserRoleService.addSysUserRoleBySysUserRole(sysUserRole);

        //2.跳转页面
        //a.用户数据
        SysUser sysUser_db = sysUserService.querySysUserByUserId(sysUserId);
        model.addAttribute("sysUser_db", sysUser_db);
        //b.分配角色数据
        List<SysRole> sysRoleList_db = sysRoleService.queryAllSysRole();
        model.addAttribute("sysRoleList_db", sysRoleList_db);

        SysUserRole sysUserRole_db=sysUserRoleService.querySysUserRoleByUserId(sysUserId);
        model.addAttribute("sysUserRole_db", sysUserRole_db);
        return "view/system/sysRole/FPRoleUI";
    }
}
